<?php
/** 命名空间 */
namespace qqv3;
/**
 * PHP SDK for  OpenAPI V3
 *
 * @version 3.0.0
 * @author open.qq.com
 * @copyright © 2011, Tencent Corporation. All rights reserved.
  * @ History:
 *               3.0.0 | nemozhang | 2011-12-12 11:11:11 | initialization
 */
/** 加载SnsSigCheck.php文件 */
require Dir_Lib    	. 'qqv3/SnsSigCheck.php';
/** 加载SnsNetwork.php文件 */
require Dir_Lib    	. 'qqv3/SnsNetwork.php';
/** 加载SnsStat.php文件 */
require Dir_Lib    	. 'qqv3/SnsStat.php';
/**
 * 提供访问腾讯开放平台 OpenApiV3 的接口
 */
class OpenApiV3
{
	public $server_name 	= '';
	
	public $openid 		= '';		// 参数
	public $openkey 		= '';		// 参数
	public $appid  		= 0;	    // 参数	
	public $appkey 		= '';
	//private $sig 		    = '';       // 参数
	public $pf 			= 'qzone';  // 参数
	
	public $format 		= 'json';	// 参数(可选)
	public $userip 		= '';  		// 参数(可选)
	
	public $stat_url       = "apistat.tencentyun.com";    // 参数(可选)
    public $stat_is        = true;                       // 参数(可选)
	/**
	 * 构造函数
	 *
	 * @param int $appid 应用的ID
	 * @param string $appkey 应用的密钥
	 */
	public function __construct($server_name,$appid,$appkey,$openid,$openkey,$userip='',$format='json',$pf='')
	{
		$this->server_name 	= $server_name;
		$this->appid 		= $appid;
		$this->appkey 		= $appkey;
		
		$this->openid 		= $openid;
		$this->openkey 		= $openkey;	
		
		$this->format 		= $format;
		$this->userip 		= $userip;
	}
	/**
	 * 设定pf
	 */
	public function set_pf($pf)
	{
		$this->pf 			= $pf;
	}
	/**
	 * 设定 stat
	 */
	public function set_stat($stat_is,$stat_url)
	{
	    $this->stat_is 		= $stat_is;
	    $this->stat_url 	= $stat_url;
	}
	/**
	 * 执行API调用，返回结果数组
	 *
	 * @param array $script_name 调用的API方法 参考 http://wiki.open.qq.com/wiki/API_V3.0%E6%96%87%E6%A1%A3
	 * @param array $params 调用API时带的参数
	 * @param string $method 请求方法 post / get
	 * @param string $protocol 协议类型 http / https
	 * @return array 结果数组
	 */
	public function api($script_name, $params, $method='post', $protocol='http')
	{
		// 检查 openid 是否为空
		if (!isset($this->openid) || empty($this->openid))
		{
			return array(
				'ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY,
				'msg' => 'openid is empty');
		}
		// 检查 openkey 是否为空
		if (!isset($this->openkey) || empty($this->openkey))
		{
			return array(
				'ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY,
				'msg' => 'openkey is empty');
		}
		// 检查 openid 是否合法
		if (!isOpenId($this->openid))
		{
			return array(
				'ret' => OPENAPI_ERROR_REQUIRED_PARAMETER_INVALID,
				'msg' => 'openid is invalid');
		}		
		// 无需传sig, 会自动生成
		unset($params['sig']);
		
		// 添加一些参数
		$params['openid']	= $this->openid;
		$params['openkey']	= $this->openkey;
		$params['appid']	= $this->appid;		
		$params['pf'] 		= $this->pf;
		$params['format'] 	= $this->format;
		$params['userip'] 	= $this->userip;
		
		// 生成签名
		$secret 		= $this->appkey . '&';
		$sig 			= makeSig($method, $script_name, $params, $secret);
		$params['sig'] 	= $sig;
		
		
		$url 			= $protocol . '://' . $this->server_name . $script_name;
		$cookie 		= array();
        
		//记录接口调用开始时间
		$start_time     = getTime();
		// 发起请求
		$ret 			= makeRequest($url, $params, $cookie, $method, $protocol);
		
		if (false === $ret['result'])
		{
			$result_array = array(
				'ret' => OPENAPI_ERROR_CURL + $ret['errno'],
				'msg' => $ret['msg']);
		}
		
		$result_array = json_decode($ret['msg'], true);
		
		// 远程返回的不是 json 格式, 说明返回包有问题
		if (is_null($result_array)) 
		{
			$result_array = array(
				'ret' => OPENAPI_ERROR_RESPONSE_DATA_INVALID,
				'msg' => $ret['msg']);
		}
		
		// 统计上报
		if ($this->is_stat)
		{
		    $stat_params = array(
		            'appid'     => $this->appid,
		            'pf'        => $params['pf'],
		            'rc'        => $result_array['ret'],
		            'svr_name'  => $this->server_name,
		            'interface' => $script_name,
		            'protocol'  => $protocol,
		            'method'    => $method,
		    );
		    statReport($this->stat_url, $start_time, $stat_params);
		}

		return $result_array;
	}
	
}

/**
 * 检查 openid 的格式
 *
 * @param string $openid openid
 * @return bool (true|false)
 */
function isOpenId($openid)
{
	return (0 == preg_match('/^[0-9a-fA-F]{32}$/', $openid)) ? false : true;
}

// end of script